import gameCtr from "../ctrs/gameCtr";
import BaseEle, { campType } from "./baseEle";

/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-16 17:51:53
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-16 17:56:16
 */
const { ccclass, property } = cc._decorator;

@ccclass

export default class dcEle extends cc.Component {
  @property({ type: cc.Animation, tooltip: "" })
  ani: cc.Animation = null;

  @property({ type: cc.Sprite, tooltip: "" })
  sp: cc.Sprite = null;

  public aoeNum: number = 2; //aoe能造成的最大伤害数量

  private num: number = 0;

  private campType: campType;

  /**
   * 设置aoe数量
   */
  public setInfo(info) {
    this.aoeNum = info.aoeNum;
    this.num = 0;
    this.campType = info.campType || 1;
    this.ani.stop();
    this.ani.play("dc");
    this.ani.once("finished", this.dcOver, this);
  }

  dcOver() {
    this.ani.stop();
    this.overHurt();
    this.ani.play("dcS");
    this.ani.once("finished", this.recover, this);
  }

  /**
   * 范围伤害
   */
  public overHurt() {

    let childs = gameCtr.ins.eleNode.children;

    for (let i: number = 0; i < childs.length; i++) {

      let ele: cc.Node = childs[i];
      // console.log("hurtBlood");
      if (!ele) {
        // console.log("hurtBlood");
        continue;
      }

      if (this.num >= this.aoeNum) break;

      let spr = ele.getComponent(BaseEle);

      if (spr.campType != this.campType && !spr.isFly && !spr.isFire && this.node.getBoundingBox().intersects(ele.getBoundingBox())) {

        let dir = this.node.x > ele.x ? 1 : -1;

        this.num++;

        // ele.getComponent(BaseEle).doFly(dir, 3);
        ele.getComponent(BaseEle).hurtBlood(1, this.campType);
      }
    }
  }

  recover() {

    this.num = 0;
    if (gameCtr.ins.poolObj['dc']) {
      gameCtr.ins.poolObj['dc'].put(this.node);
    } else {
      this.node.removeFromParent();
    }
  }
}